package com.wjggy.core.action.posts;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.wjggy.core.action.view.PostsView;
import com.wjggy.core.common.page.Page;
import com.wjggy.core.common.page.PageUtil;
import com.wjggy.core.common.springview.ViewUtil;
import com.wjggy.core.entities.Forums;
import com.wjggy.core.service.posts.CategoryService;
import com.wjggy.core.service.posts.ForumService;


 /**
 *  <p>Description: 栏目管理 </p>
 *  <p>Copyright:Copyright (c) 2012  </p>
 *  <p>Company:wjggy  </p>
 *  @author xiaofan.yu  
 *  @date   2012-2-3 下午05:48:18    
 *  @project  wjggy
 *  @version  1.0.0
 *  @see   
 *  @since 1.6 
 */
@Controller
@RequestMapping("/admin/forum")
public class ForumsAction {
	@Resource(name="forumService")
	ForumService forumService;
	@Resource(name="categoryService")
	CategoryService categoryService;
	/**
	 * 
	 *  @describe 栏目列表
	 *  @param request
	 *  @param response
	 *  @return
	 */
	@RequestMapping(value="",method=RequestMethod.GET)
	public ModelAndView forumList(Forums forums,HttpServletRequest request, HttpServletResponse response) {
		 ModelAndView model=ViewUtil.findView(PostsView.FORUMS_URL);
		 Page page= PageUtil.createPage(request,"");
		 page.setResult(forumService.findForums(forums,page));
		 model.addObject("forum", forums);
		 model.addObject("categorys", categoryService.findCategory(null));
		 model.addObject("page", page);
		return  model;
	}
	/**
	 * @describe 栏目新增页面
	 * @return
	 */
	@RequestMapping(value="add",method=RequestMethod.GET)
	public ModelAndView forumAdd() {
		ModelAndView model=ViewUtil.findView(PostsView.FORUMS_ADD_URL);
		model.addObject("categorys", categoryService.findCategory(null));
		return model;
	}
	/**
	 * @describe 栏目新增 
	 * @param forums
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="add",method=RequestMethod.POST)
	public ModelAndView forumAdded(Forums forums,HttpServletRequest request, HttpServletResponse response) {
		forums.setForumstops(0l);
		forumService.addForums(forums);
		return forumList(forums,request,response);
	}
	/**
	 * @describe 栏目更新页面
	 * @return
	 */
	@RequestMapping(value="update/{id}",method=RequestMethod.GET)
	public ModelAndView forumUpdate(@PathVariable Long id) {
		ModelAndView model=ViewUtil.findView(PostsView.FORUMS_UPDATE_URL);
		Forums forums=forumService.getForums(id);
		model.addObject("forum", forums);
		model.addObject("categorys", categoryService.findCategory(null));
		return model;
	}
	/**
	 * @describe 栏目更新
	 * @param forums
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="update",method=RequestMethod.POST)
	public ModelAndView forumUpdated(Forums forums,HttpServletRequest request, HttpServletResponse response) {
		forumService.updateForums(forums);
		return forumList(forums,request,response);
	}
	/**
	 * @describe 栏目删除
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/delete/{id}")
	public String delete(@PathVariable Long id){
		Forums forums=new Forums();
		forums.setForumsid(id);
		forumService.deleteForums(forums);
		return "redirect:/admin/forum";
	}
}

